package com.bjtu.literatureclient.base.result;


import org.apache.commons.lang3.StringUtils;

/**
 * @author caiyuli
 * @date 2020-02-18 20:01
 */

public class Result<T>{
    protected static final int SUCCESS = 0;
    protected static final int SYS_ERROR = -1;

    private T data;
    private int code;
    private String message;

    public Result() {
    }

    public Result(T data, int code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    /**
     * 成功时候的调用
     *
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data, SUCCESS, "成功");
    }

    public static <T> Result<T> success() {
        return successMsg(null);
    }

    public static <T> Result<T> success(T data,SuccessEnum successEnum) {
        return new Result<T>(data, successEnum.getCode(), successEnum.getMessage());
    }

    public static <T> Result<T> fail(T data,FailEnum failEnum) {
        return new Result<T>(data, failEnum.getCode(), failEnum.getMessage());
    }

    /**
     * 成功，不需要传入参数
     *
     * @return
     */
    public static <T> Result<T> successMsg(String msg) {
        return new Result<T>(null, SUCCESS, StringUtils.defaultIfEmpty(msg, "成功"));
    }

    /**
     * 失败时候的调用
     *
     * @return
     */
    public static <T> Result<T> fail(String msg) {
        return new Result<T>(null, SYS_ERROR, StringUtils.defaultIfEmpty(msg, "失败"));
    }

    /**
     * 失败时候的调用,扩展消息参数
     *
     * @param code
     * @param msg
     * @return
     */
    public static <T> Result<T> fail(int code, String msg) {
        code = (code == SUCCESS ? code + 1 : code);
        return new Result<T>(null, code, StringUtils.defaultIfEmpty(msg, "失败"));
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
